<?php

if(!defined('_HTML_TABLE_LIST_')) {
define('_HTML_TABLE_LIST_', 1);

function itstock_html_table_list_navigation(&$data, $right_column = '&nbsp;') {
?>
  <tr>
    <td>
    <b><?php
    itstock_html_navigation($data);
    ?></b>
    </td>
    <td style="text-align: right;"><?php echo $right_column;?></td>
  </tr>
<?php
}

function itstock_html_table_list_headers(&$headers) {
?>
  <thead>
    <tr>
      <?php
      foreach($headers as $th) {
          echo "<th>".$th."</th>";
      }
      ?>
    </tr>
  </thead>
  <tfoot>
    <tr><th style="border-radius: 0px 0px 3px 3px; height: 5px;" colspan="<?php echo count($headers)?>"></th></tr>
  </tfoot>
<?php
}

function itstock_html_table_list_links($links, &$data, $class = false) {
  foreach($links as $link) {
    if(isset($link['id'])){
      $enc_id = itstock_encode($data[$link['id']]);
      if(isset($link['link'])){
        $link['link'] = sprintf($link['link'], $enc_id);
      }
      if(isset($link['onclick'])){
        $link['onclick'] = sprintf($link['onclick'], $enc_id);
      }
    }
    echo '<td class="list_center">'.itstock_html_links_img($link).'</td>';
  }
}

function itstock_html_table_list_data(&$data, $list = true , $nav = true, $table_list = false, $right_column = '&nbsp;') {
  if(itstock_search_all($data)){
    if($nav) {
      itstock_html_table_list_navigation($data, $right_column);
    }
    ?>
    <tr>
    <td colspan="2">
    <table class="list">
    <?php
    itstock_html_table_list_headers($data['table_headers']);
    echo '<tbody>';
    foreach($data['data'] as $i => &$item){
      echo '<tr>'.($list?'<td>'.((isset($data['page_idx'])?$data['page_idx']*_ITSTOCK_MAX_ROWS_+$i:$i)+1).'</td>':'');
      foreach($data['table_indexes'] as $idx) {
        echo '<td>'.$item[$idx].'</td>';
      }
      if($table_list){
        itstock_html_table_list_links($data['table_actions'], $item, 'list_center');
      }else{
        $enc_id = itstock_encode($item['id']);
        foreach($data['table_actions'] as $action) {
          printf('<td class="list_center">'.$action.'</td>', $enc_id);
        }
      }
      echo '</tr>';
    }
    echo '</tbody>';
  } else {
    echo '<tr><td><table><tr><td>'.$data['table_error'].'</td></tr>';
  }
  echo '</table></td></tr>';
}

}
?>
